# 标准库
import time

# 库引用
from sqlalchemy import Column, Integer, VARCHAR

# 业务代码
from hzb.database import db


class WxUnionIdManageModel(db.Model):
    # TODO 微信union_id的存储，量大后使用redis
    # 表名
    __tablename__ = "hzb_wx_union_id_manage"
    id = Column('id', Integer, primary_key=True, comment='')
    union_id = Column('union_id', VARCHAR(255), comment="微信的union_id")
    create_time = Column('create_time', Integer, comment='union_id保存时间')

    @staticmethod
    def is_union_id(union_id):
        _obj = WxUnionIdManageModel.query.filter_by(union_id=union_id).first()
        if _obj is not None:
            return True
        return False

    @staticmethod
    def save_union_id(union_id):
        _obj = WxUnionIdManageModel.query.filter_by(union_id=union_id).first()
        if _obj is None:
            _obj = WxUnionIdManageModel()
            _obj.create_time = int(time.time())
            _obj.union_id = union_id
            db.session.add(_obj)
            db.session.commit()
        return True
    pass

